import 'dart:convert';

import 'package:bot_toast/bot_toast.dart';
import 'package:common_utils/common_utils.dart';
import 'package:exams_app/gen_a/A.dart';
import 'package:exams_app/login_module/register/register_getcode_page.dart';
import 'package:exams_app/net/Address.dart';
import 'package:exams_app/net/DioManager.dart';
import 'package:exams_app/net/persisten_storage.dart';
import 'package:exams_app/widget/hexcolor.dart';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({Key key}) : super(key: key);

  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> with SingleTickerProviderStateMixin {


  // final LoginLogic logic = Get.put(LoginLogic());


  var tf = TextEditingController();
  // FocusNode _focusNode= FocusNode();


  @override
  void initState() {
    super.initState();
    // FocusScope.of(context).requestFocus(_focusNode);

  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: new IconButton(
          icon: Image.asset(A.assets_arrback_icon),
          onPressed: () => {
            Navigator.of(context).pop('刷新')
          },
        ),
      ),
      body: Column(
        crossAxisAlignment:CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(top: 20,left: 20),
            child: Text('注册',style: TextStyle(fontSize: 24,fontWeight: FontWeight.w500),),
          ),
          Container(
            margin: EdgeInsets.only(top: 5,left: 20),
            child: Text('未注册的手机号验证后自动注册',style: TextStyle(fontSize: 12,color: HexColor('#999999')),),
          ),


          Container(
            margin: EdgeInsets.only(left: 20,top: 50),
            child: TextField(
              keyboardType: TextInputType.phone,
              controller: tf,
              // focusNode: _focusNode,
              autofocus: true,
              inputFormatters: <TextInputFormatter>[
                WhitelistingTextInputFormatter.digitsOnly,//只输入数字
                   LengthLimitingTextInputFormatter(12)//限制长度
                ],
              style: TextStyle(
                fontSize: 24,
                color: HexColor('333333')
              ),
              decoration: InputDecoration(
                border: InputBorder.none,
                hintStyle: TextStyle(fontSize: 24,color: HexColor('#CCCCCC')),
                hintText: '请输入手机号',
              ),
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: 50,right: 25,left: 25),
            width: Get.width,
            height: 45,
            child: RaisedButton(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(22),
              ),
              color:Theme.of(context).primaryColor,
              onPressed: ()async{
                if(tf.text.isEmpty){
                  // Toast.toast(Get.overlayContext,msg: '请输入正确的手机号码');
                  BotToast.showText(text: '请输入正确的手机号码');

                  return;
                }
                if(!RegexUtil.isMobileSimple(tf.text)){
                  BotToast.showText(text: '请输入正确的手机号码');
                  return;
                }

                requestDataSendCode(tf.text).then((value) {
                  if(value['errcode'] == 0){

                    BotToast.showText(text: value['message']);
                    Get.to(RegisterGetCodePage(tf.text));
                  }
                });


                // Get.to(RegisterGetCodePage(tf.text));

                // logic.requestDataSendCode(tf.text).then((value) {
                //   if(value['errocode']==0){
                //     Get.to(RegisterGetCodePage(tf.text));
                //   }
                // });
              },
              child: Text('获取验证码',style: TextStyle(color: Colors.white),),
            ),
          ),
        ],
      ),
    );
  }


  Future requestDataSendCode(var tf)async{
    var params = {
      'mobile':tf,
      'name':'登录确认验证码',
      'type':'0',
      'n':'6',
    };
    String result = await DioUtils(url: await PersistentStorage().getStorage('urlStr')).post(Address.sendCode,data:params);

    return jsonDecode(result);

  }
}
